package com.gnerv.cms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gnerv.cms.dto.SiteDTO;
import com.gnerv.cms.dto.SiteSaveDTO;
import com.gnerv.cms.dto.SiteSearchDTO;
import com.gnerv.cms.entity.Site;
import com.gnerv.cms.mapper.SiteMapper;
import com.gnerv.cms.service.ISiteService;
import com.gnerv.cms.utils.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 基础平台-站点配置表 服务实现类
 * </p>
 *
 * @author Gnerv LiGen
 * @since 2019-10-10
 */
@Slf4j
@Service
public class SiteServiceImpl extends ServiceImpl<SiteMapper, Site> implements ISiteService {

    @Resource
    private SiteMapper siteMapper;

    @Override
    public boolean saveSite(SiteSaveDTO siteSaveDTO) {
        if (StringUtils.isEmpty(siteSaveDTO.getUkId())) {
            this.createSite(siteSaveDTO);
        } else {
            this.updateSite(siteSaveDTO);
        }
        return true;
    }

    @Override
    public boolean createSite(SiteSaveDTO siteSaveDTO) {
        siteSaveDTO.setUkId(UUIDUtils.getLower());
        Site site = siteSaveDTO.convert();
        siteMapper.insert(site);
        return true;
    }

    @Override
    public boolean deleteSite(String siteId) {
        siteMapper.deleteById(siteId);
        return true;
    }

    @Override
    public boolean updateSite(SiteSaveDTO siteSaveDTO) {
        siteMapper.updateById(siteSaveDTO.convert());
        return true;
    }

    @Override
    public boolean updateSiteStatus(String siteId, Integer status) {
        siteMapper.updateSiteStatus(siteId, status);
        return true;
    }

    @Override
    public SiteDTO selectSite(String siteId) {
        return siteMapper.selectSite(siteId);
    }

    @Override
    public SiteDTO selectSite(SiteSearchDTO siteSearchDTO) {
        return siteMapper.selectSiteByNameOrCode(siteSearchDTO);
    }

    @Override
    public List<SiteDTO> listSite(SiteSearchDTO siteSearchDTO) {
        List<SiteDTO> sites = siteMapper.listSite(siteSearchDTO);
        return sites;
    }

}
